package ano.printer;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author oldliu
 * @since 1.0
 */
@Service("b5")//名称是 B5，12*8
public class Paper {
    @Value("12")
    private int lines;//一页多少行
    @Value("8")
    private int words;//一行多少个字

    public void setLines(int lines) {
        this.lines = lines;
    }

    public void setWords(int words) {
        this.words = words;
    }

    //用控制台模拟纸张
    //一行到头换行
    //一页到头换纸
    public void print(String s) {
        char[] cr = s.toCharArray();
        int line = 1;
        int word = 1;
        int page = 1;
        for (char c : cr) {
            System.out.print(c);
            word++;
            if (word >= this.words) {//换行
                line++;
                word = 1;
                System.out.println();
                if (line >= this.lines) {//换纸张
                    //输出页码
                    System.out.println("----" + (page++) + "---");
                    line = 1;
                }
            }
        }
        if (word != 1 || line != 1) {
            System.out.println("\n----" + page + "---");
        }
    }

    @Override
    public String toString() {
        return "纸张{" +
                "行数：" + lines +
                ", 每行字数：" + words +
                '}';
    }
}
